package com.menggudf.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.menggudf.dao.SysCaseHistoryDao;
import com.menggudf.entity.SysCaseHistory;
import com.menggudf.model.ResultModel;
import com.menggudf.model.SysCaseHistoryModel;
import com.menggudf.service.SysCaseHistoryService;

@Service
public class SysCaseHistoryServiceImpl implements SysCaseHistoryService{

    private static final Logger LOGGER = LoggerFactory
            .getLogger(SysCaseHistoryServiceImpl.class);
    
    @Autowired
    SysCaseHistoryDao sysCaseHistoryDao;

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> getList(String userName,int pageSize, int offset) {
        List<SysCaseHistory> list = sysCaseHistoryDao.getList(userName, pageSize, offset);
        long count = sysCaseHistoryDao.getListCount(userName);
        List<SysCaseHistoryModel> mlist = new ArrayList<SysCaseHistoryModel>();
        for(SysCaseHistory s : list){
            SysCaseHistoryModel sm = new SysCaseHistoryModel();
            BeanUtils.copyProperties(s, sm);
            mlist.add(sm);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", mlist);
        return map;
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel editSysCaseHistoryActive(int id, String active) {
        SysCaseHistory s = sysCaseHistoryDao.findById(id);
        if(s==null){
            return ResultModel.fail("修改失败,记录为空", null);
        }
        try {
            s.setAdvice(active);
            s.setStatus(1);
            sysCaseHistoryDao.persist(s);
            return ResultModel.success("修改成功", null);
        } catch (Exception e) {
            LOGGER.error("editSysCaseHistoryActive fail",e);
        }
        return ResultModel.fail("修改失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel toSaveSysHistory(
            String sysptoms,
            String scheme,
            String illness,
            String medicine,
            Integer role,
            String userCode) {
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");
            SysCaseHistory sysCaseHistory=new SysCaseHistory();
            sysCaseHistory.setCreateTime(new Date());
            sysCaseHistory.setCaseHistoryName(sdf.format(new Date())+"【"+illness+"】");
            sysCaseHistory.setInquiryResult(illness);
            sysCaseHistory.setIsSysCase(1);
            sysCaseHistory.setMedicine(medicine);
            sysCaseHistory.setScheme(scheme);
            sysCaseHistory.setSysptom(sysptoms);
            
            //userCode和role做唯一键
            sysCaseHistory.setUserCode(userCode);
            sysCaseHistory.setUserName(userCode);
            sysCaseHistory.setRole(role);
            
            sysCaseHistoryDao.persist(sysCaseHistory);
            return ResultModel.success();
        } catch (Exception e) {
            LOGGER.error("toSaveSysHistory fail",e);
        }
        return ResultModel.fail();
    }
}
